from module.dynamic_module.k_chart.view.ele_view import KLinesWinView, VolumeWinView, FactorWinView
from module.dynamic_module.k_chart.view.console_view import ConsoleView
from conf import conf
from core.constant import *
from tools.view import frame_canvas_del
import tkinter as tk


class SuperView:
    """顶级窗口类"""
    def __init__(self, super_model):
        self.super_model = super_model
        # self.root = tk.Tk()
        self.root = tk.Toplevel()
        self.root.title("DDQuant K线图")
        self.root.geometry(f"{conf.KChart.super_view_size.value[0]}x{conf.KChart.super_view_size.value[1]}")
        # 创建下级容器
        self.console_frame = None
        self.k_lines_frame = None
        self.volume_frame = None
        self.factor_frame_ls = None

        self.console_view = None
        self.k_lines_view = None
        self.volume_view = None
        self.factor_view_ls = None

    def view_instantiate(self):
        # 预创建相应的frame
        row_num = 2 + self.super_model.factor_model_num
        self.console_view_instantiate(row_num)
        self.ele_view_instantiate()

    def console_view_instantiate(self, row_num):
        self.console_frame = tk.Frame(self.root, width=self.super_model.console_view_size[0], height=self.super_model.console_view_size[1])
        self.console_frame.grid(row=0, column=1, rowspan=row_num, sticky="nsew")

        # 实例化Console窗口视图类
        self.console_view = ConsoleView(self, self.console_frame, self.super_model.console_model)
        self.console_view.sec_init()
        pass

    def ele_view_instantiate(self):
        if self.k_lines_frame is None:
            self.k_lines_frame = tk.Frame(self.root, width=self.super_model.k_lines_view_size[0], height=self.super_model.k_lines_view_size[1])
            self.k_lines_frame.grid_rowconfigure(0, weight=1)
            self.k_lines_frame.grid_columnconfigure(0, weight=1)
            self.k_lines_frame.grid(row=0, column=0, sticky="nsew")
            # 实例化K线图视图类
            self.k_lines_view = KLinesWinView(self, self.k_lines_frame, self.super_model.k_lines_model)
            self.k_lines_view.sec_init()
        else:
            self.k_lines_frame.config(width=self.super_model.k_lines_view_size[0], height=self.super_model.k_lines_view_size[1])
            self.k_lines_frame.grid_propagate(False)  # 防止内部组件影响尺寸
            # 更改大小
            self.k_lines_view.ele_model.layout_info(self.super_model.k_lines_view_size)
            pass

        if self.volume_frame is None:
            self.volume_frame = tk.Frame(self.root, width=self.super_model.volume_view_size[0], height=self.super_model.volume_view_size[1])
            self.volume_frame.grid_rowconfigure(0, weight=1)
            self.volume_frame.grid_columnconfigure(0, weight=1)
            self.volume_frame.grid(row=1, column=0, sticky="nsew")
            # 实例化交易量图视图类
            self.volume_view = VolumeWinView(self, self.volume_frame, self.super_model.volume_model)
            self.volume_view.sec_init()
        else:
            self.volume_frame.config(width=self.super_model.volume_view_size[0],
                                      height=self.super_model.volume_view_size[1])
            # self.volume_frame.grid_propagate(False)  # 防止内部组件影响尺寸
            # 更改大小
            self.volume_view.ele_model.layout_info(self.super_model.volume_view_size)

            pass

        if self.factor_frame_ls is None:
            self.factor_frame_ls = []
            for i in range(self.super_model.factor_model_num):
                row = i + 2
                factor_frame_x = tk.Frame(self.root, width=self.super_model.factor_view_size[0], height=self.super_model.factor_view_size[1])
                factor_frame_x.grid_rowconfigure(0, weight=1)
                factor_frame_x.grid_columnconfigure(0, weight=1)
                factor_frame_x.grid(row=row, column=0, sticky="nsew")
                self.factor_frame_ls.append(factor_frame_x)

            # 实例化因子图视图类
            self.factor_view_ls = []
            for i, factor_model in enumerate(self.super_model.factor_model_ls):
                factor_view = FactorWinView(self, self.factor_frame_ls[i], factor_model)
                factor_view.sec_init()
                self.factor_view_ls.append(factor_view)
        else:
            for i, frame_o in enumerate(self.factor_frame_ls):
                frame_o.config(width=self.super_model.factor_view_size[0],
                               height=self.super_model.factor_view_size[1])
                frame_o.grid_propagate(False)  # 防止内部组件影响尺寸
                # 更改大小
                self.factor_view_ls[i].ele_model.layout_info(self.super_model.factor_view_size)
            pass



        pass

    def view_mobilize(self):
        # 执行窗口视图类下的相关方法
        # 执行Console窗口视图类方法
        self.console_view.layout()
        self.console_view.fill_text()
        # 绘制
        self.ele_view_draw()

        pass

    def view_update(self):
        self.console_view.fill_text()
        # 清空绘图界面canvas的像素
        self.ele_canvas_del()
        # 绘制
        self.ele_view_draw()

    def ele_canvas_del(self):
        # 清空绘图界面canvas的像素
        # K线图
        frame_canvas_del(self.k_lines_view.frame)
        # Volume图
        frame_canvas_del(self.volume_view.frame)
        # Factor图
        for factor_view in self.factor_view_ls:
            frame_canvas_del(factor_view.frame)

    def ele_view_draw(self):
        # 绘制K线图
        self.k_lines_view.ele_model.legend_ls = []
        self.k_lines_view.draw_y_line()  # 绘制y轴参考线
        self.k_lines_view.draw_k()
        for i, factor in enumerate(self.k_lines_view.ele_model.factor_ls):
            if factor in [SigType.Buy.value, SigType.Sell.value, SigType.Mess.value]:
                continue
            color = conf.KChart.factor_order_color_ls.value[i]
            if factor not in conf.KChart.point_factor.value:
                self.k_lines_view.ele_model.add_legend(factor, color, FactorShowCategory.Line)
                self.k_lines_view.draw_factor_line(factor, color)
            else:
                self.k_lines_view.ele_model.add_legend(factor, color, FactorShowCategory.Point)
                self.k_lines_view.draw_factor_point(factor, color)
        # K线图坐标轴图例
        self.k_lines_view.show_attach()

        # 绘制交易量图
        self.volume_view.ele_model.legend_ls = []
        self.volume_view.draw_y_line()  # 绘制y轴参考线
        self.volume_view.draw_volume()
        for i, factor in enumerate(self.volume_view.ele_model.factor_ls):
            color = conf.KChart.factor_order_color_ls.value[i]
            if factor not in conf.KChart.point_factor.value:
                self.volume_view.ele_model.add_legend(factor, color, FactorShowCategory.Line)
                self.volume_view.draw_factor_line(factor, color)
            else:
                self.volume_view.ele_model.add_legend(factor, color, FactorShowCategory.Point)
                self.volume_view.draw_factor_point(factor, color)
        # Volume图坐标轴图例
        self.volume_view.show_attach()

        # 绘制因子图
        for factor_view in self.factor_view_ls:
            factor_view.ele_model.legend_ls = []
            factor_ls = factor_view.ele_model.factor_ls
            factor_view.draw_y_line()  # 绘制y轴参考线
            for j, factor in enumerate(factor_ls):
                color = conf.KChart.factor_order_color_ls.value[j]
                if factor in conf.KChart.point_factor.value:
                    factor_view.ele_model.add_legend(factor, color, FactorShowCategory.Point)
                    factor_view.draw_factor_point(factor=factor, color=color)
                elif factor in conf.KChart.tank_factor.value:
                    factor_view.ele_model.add_legend(factor, color, FactorShowCategory.Tank)
                    factor_view.draw_factor_tank(factor=factor, color_ls=conf.KChart.tank_color_ls.value)
                else:
                    factor_view.ele_model.add_legend(factor, color, FactorShowCategory.Line)
                    factor_view.draw_factor_line(factor=factor, color=color)

            # Factor图坐标轴图例
            factor_view.show_attach()

        # 顶端绘制sig图
        self.k_lines_view.draw_buy()
        self.k_lines_view.draw_sell()
        self.k_lines_view.draw_mess()

    def mainloop(self):
        self.root.mainloop()
        pass




